package com.broadengate.view.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.broadengate.bean.DepartmentInfo;
import com.broadengate.bean.RuleInfo;
import com.broadengate.bean.SessionInfo;
import com.broadengate.bean.UserInfo;
import com.broadengate.service.DepartmentService;
import com.broadengate.service.UserService;
import com.broadengate.util.JSONResult;
import com.broadengate.util.ServiceException;

@Controller
@RequestMapping("/LoginController")
public class LoginController
{
    
    private final static Logger log = Logger.getLogger(LoginController.class);
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private DepartmentService departmentService;
    
    @RequestMapping("/login")
    public ModelAndView login(UserInfo user, HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
    {
        log.info("userName=" + user.getUserName() + "password=" + user.getPassword());
        //如果用户已经登录则直接跳转到INDEX页面
        SessionInfo session = (SessionInfo) request.getSession().getAttribute("session");
        
        if (session != null && session.getUserId() != 0)
        {
            return new ModelAndView("index", modelMap);
        }
        
        ModelMap returnMap = new ModelMap();
        UserInfo logUserInfo = new UserInfo();
        //用户名密码检测
        try
        {
            logUserInfo = userService.login(user);
        }
        catch (ServiceException e)
        {
            returnMap.addAttribute("mesg", "登录失败，密码或账户错误!");
            return new ModelAndView("login", returnMap);
        }
        
        //获取用户一级目录列表
        List<RuleInfo> ruleList = userService.getUserRuleList(logUserInfo.getUserId(), "1");
        
        //获取系统部门树
        List<DepartmentInfo> dptList= departmentService.getDepartmentById(0);
        
        //将相关信息放入到session中
        session = new SessionInfo();
        BeanUtils.copyProperties(logUserInfo, session);
        session.setRuleList(ruleList);
        session.setDepartmentTree(dptList);
        
        request.getSession().setAttribute("session", session);
        
        return new ModelAndView("index", modelMap);
    }
    
    @RequestMapping("/logout")
    public ModelAndView loginout(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
    {
        HttpSession session = request.getSession();
        JSONResult j = new JSONResult();
        if (session != null)
        {
            session.invalidate();
        }
        
        return new ModelAndView("login", modelMap);
    }
    
}
